package tech.abing.spider.task.crawler.amac;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.management.JMException;

import tech.abing.spider.CrawTaskBuilder;
import tech.abing.spider.CrawlManager;
import tech.abing.spider.CrawlTask;
import tech.abing.spider.component.DynamicEntrance;
import tech.abing.spider.component.listener.TaskLifeListener;
import tech.abing.spider.downloader.impl.DefaultPageDownloader;
import tech.abing.spider.monitor.SpiderMonitor;
import tech.abing.spider.request.PageRequest;
import tech.abing.spider.request.PageRequest.Method;
import tech.abing.spider.request.PageRequest.PageEncoding;
import tech.abing.spider.request.PageRequest.PostType;
import tech.abing.spider.request.StartContext;
import tech.abing.spider.task.constants.DictEnum;
import tech.abing.spider.task.pageprocessor.amac.AssetSupportSpecialProcessor;
import tech.abing.spider.task.pageprocessor.amac.FundInfoProcessor;
import tech.abing.spider.task.pipeline.amac.AssetSupportSpecialPipeline;
import tech.abing.spider.task.utils.TaskUtil;
import tech.abing.spider.util.SqlUtil;

/**
 * 抓取资产支持专项计划
 * 
 * @author ZZW
 *
 */
public class AssetSupportSpecialSpider {

	/**
	 * @param args
	 */
	private static SqlUtil sqlUtil = null;

	public static void start(int thread, int politenessDelay) {

		CrawlManager manager = CrawlManager.getInstance();

		CrawTaskBuilder builder = manager.prepareCrawlTask(DictEnum.TASK_TYPE_ASSET_SUPPORT_SPECIAL.getName(), DefaultPageDownloader.class);
		builder.useThread(thread);
		builder.useDynamicEntrance(DynamicEntranceImpl.class);
		builder.usePageEncoding(PageEncoding.UTF8);
		builder.usePageRetryCount(3);
		builder.usePipeline(AssetSupportSpecialPipeline.class).setPolitenessDelay(politenessDelay);
		builder.useTaskLifeListener(new TaskLifeListener() {

			@Override
			public void onStart(CrawlTask task) {
				if (sqlUtil == null) {
					sqlUtil = SqlUtil.getInstance();
				}
				sqlUtil.deleteForConn("DELETE FROM " + DictEnum.TASK_TYPE_ASSET_SUPPORT_SPECIAL.getCode());
//				sqlUtil.deleteForConn("DELETE FROM " + DictEnum.TASK_TYPE_ASSET_SUPPORT_SPECIAL_DETAIL.getCode() );
				System.out.println("================开始");
			}

			@Override
			public void onFinished(CrawlTask task) {
				task.getPipeline().destory();
				sqlUtil.closeConn();
				sqlUtil = null;
				System.out.println("================完成");
			}

		});

		CrawlTask task = builder.build();
		manager.start(task);
	}

	public static void start() {
		start(20, 200);
	}

	public static final class DynamicEntranceImpl extends DynamicEntrance {

		@Override
		public List<StartContext> loadStartContext() {
			StartContext context = new StartContext();
			String url = "http://ba.amac.org.cn/pages/amacWeb/ab-special-plan!list.action";
			PageRequest req = context.createPageRequest(url, AssetSupportSpecialProcessor.class);
			
			req.setMethod(Method.POST);
			req.setPostType(PostType.FORMDATA);
			req.setReqPayloadParams("{}");
			req.putHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
			req.putHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; Touch; rv:11.0) like Gecko");
			req.putHeader("Referer", "http://ba.amac.org.cn/pages/amacWeb/abs-web-list.html");
			req.putHeader("X-Requested-With", "XMLHttpRequest");
			req.putHeader("Host", "gs.amac.org.cn");
			req.putHeader("Accept", "application/json, text/javascript, */*; q=0.01");
			req.putHeader("Accept-Language", "zh-CN,zh;q=0.8");
			req.putHeader("Cache-Control", "no-cache");
			
			
			req.putParams("nd", TaskUtil.getRandomStrByDate());
			req.putParams("page.pageSize", "50");
			req.putParams("page.pageNo", "1");
			
			req.putParams("page.sqlCKey", "SIZE_ABS_PUBLICITY_WEB");
			req.putParams("_search", "false");
			req.putParams("page.orderBy", "AT_AUDIT_DATE");
			req.putParams("page.order", "desc");
			
			req.putParams("filter_LIKES_ASPI_NAME", "");
			req.putParams("filter_GES_AT_AUDIT_DATE", "");
			req.putParams("filter_LES_AT_AUDIT_DATE", "");
			req.putParams("page.searchFileName", "publicity_abs_web");
			req.putParams("page.sqlKey", "PAGE_ABS_PUBLICITY_WEB");
			
			context.injectSeed(req);
			
			return Arrays.asList(context);
		}

	}

}